﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Xml;

namespace ImageEditor
{
    public partial class NewsForm : Form
    {
        public NewsForm()
        {
            InitializeComponent();
            InitializeForm();
        }

        private void InitializeForm()
        {
            DisplayUpdatesNewsItems();
            DisplayCDCPagesNewsFeed();
            this.Show();
        }

        private void DisplayUpdatesNewsItems()
        {
            int count = 20;
            ArrayList stories = GetUpdatesNewsFeed();
            for (int i = 0; i < stories.Count; i++)
            {
                NewsItem item = (NewsItem)stories[i];

                LinkLabel titleLabel = new LinkLabel();
                Label publishDate = new Label();

                titleLabel.Font = new Font(FontFamily.GenericSerif, 12);
                titleLabel.Location = new Point(10, count);
                titleLabel.AutoSize = true;
                titleLabel.Text = item.Title;
                titleLabel.LinkColor = Color.Blue;
                titleLabel.Links.Add(0, item.Link.Length, item.Link);
                titleLabel.LinkClicked +=
                        new LinkLabelLinkClickedEventHandler(titleLabel_LinkClicked);
                titleLabel.Enabled = true;

                publishDate.AutoSize = true;
                publishDate.Location = new Point(20, count + 25);
                publishDate.Text = item.PubDate;
                publishDate.ForeColor = Color.Black;

                this.influenzaUpdatesPanel.Controls.Add(titleLabel);
                this.influenzaUpdatesPanel.Controls.Add(publishDate);
                this.influenzaUpdatesPanel.BackColor = Color.White;
                this.influenzaUpdatesPanel.AutoScroll = true;

                count += 55;
            }
        }

        private void DisplayCDCPagesNewsFeed()
        {
            int count = 20;
            ArrayList stories = GetCDCPagesNewsFeed();
            for (int i = 0; i < stories.Count; i++)
            {
                NewsItem item = (NewsItem)stories[i];

                LinkLabel titleLabel = new LinkLabel();
                Label publishDate = new Label();

                titleLabel.Font = new Font(FontFamily.GenericSerif, 12);
                titleLabel.Location = new Point(10, count);
                titleLabel.AutoSize = true;
                titleLabel.Text = item.Title;
                titleLabel.LinkColor = Color.Blue;
                titleLabel.Links.Add(0, item.Link.Length, item.Link);
                titleLabel.LinkClicked +=
                        new LinkLabelLinkClickedEventHandler(titleLabel_LinkClicked);
                titleLabel.Enabled = true;

                publishDate.AutoSize = true;
                publishDate.Location = new Point(20, count + 25);
                publishDate.Text = item.PubDate;
                publishDate.ForeColor = Color.Black;

                this.CDCPagesPanel.Controls.Add(titleLabel);
                this.CDCPagesPanel.Controls.Add(publishDate);
                this.CDCPagesPanel.BackColor = Color.White;
                this.CDCPagesPanel.AutoScroll = true;

                count += 55;
            }
        }

        private ArrayList GetUpdatesNewsFeed()
        {
            ArrayList newsItems = new ArrayList();
            String[] tagNames = { "item", "title", "description", "link", "pubDate" };
            String URL = "http://www2c.cdc.gov/podcasts/createrss.asp?t=r&c=20/";
            ArrayList nodeLists = DataManagement.XMLManager.GetXMLNodeLists(URL, tagNames);
            XmlNodeList items = (XmlNodeList)nodeLists[0];
            XmlNodeList titles = (XmlNodeList)nodeLists[1];
            XmlNodeList descriptions = (XmlNodeList)nodeLists[2];
            XmlNodeList links = (XmlNodeList)nodeLists[3];
            XmlNodeList pubDates = (XmlNodeList)nodeLists[4];
            for (int i = 0; i < items.Count; i++)
            {
                newsItems.Add(
                    new NewsItem(
                        titles[i].InnerText, descriptions[i].InnerText, links[i].InnerText, pubDates[i].InnerText
                        )
                    );
            }
            return newsItems;
        }

        private ArrayList GetCDCPagesNewsFeed()
        {
            ArrayList newsItems = new ArrayList();
            String[] tagNames = { "entry", "title", "summary", "updated", "link" };
            String URL = "http://t.cdc.gov/feed.aspx?tpc=26829&days=90/";
            ArrayList nodeLists = DataManagement.XMLManager.GetXMLNodeLists(URL, tagNames);
            XmlNodeList entries = (XmlNodeList)nodeLists[0];
            XmlNodeList titles = (XmlNodeList)nodeLists[1];
            XmlNodeList summaries = (XmlNodeList)nodeLists[2];
            XmlNodeList updated = (XmlNodeList)nodeLists[3];
            XmlNodeList links = (XmlNodeList)nodeLists[4];
            for (int i = 1; i < entries.Count; i++)
            {
                newsItems.Add(
                    new NewsItem(
                        titles[i].InnerText, summaries[i].InnerText, links[i].Attributes.Item(1).Value, updated[i].InnerText
                        )
                    );
            }
            return newsItems;
        }

        void titleLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            String target = e.Link.LinkData as String;
            if (target != null)
            {
                System.Diagnostics.Process.Start(target);
            }
        }
    }
}
